import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

typedef SearchHandler = Function(BuildContext context, String searchMessage);

class ContentSearchView extends StatefulWidget {

  final String? prefix;
  final SearchHandler searchHandler;
  final String? defaultText;

  ContentSearchView({required this.searchHandler,  this.prefix, this.defaultText});

  @override
  State<StatefulWidget> createState() => _ContentSearchState();

}

class _ContentSearchState extends State<ContentSearchView> {

  final controller = TextEditingController();
  String _searchText = '';

  @override
  void initState() {
    super.initState();
    if (widget.defaultText != null) {
      controller.text = widget.defaultText??'';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Row(children: [
      Expanded(
          child: Container(
              height: 30,
              decoration: const BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(10))),
              child: Row(
                children: [
                  Icon(Icons.search, color: Colors.grey, size: 18),
                  if (widget.prefix != null && widget.prefix != '') Padding(padding: EdgeInsets.only(left: 5, right: 5),
                    child: Text(widget.prefix!, style: TextStyle(color: Colors.green),),),
                  Expanded(
                    child: TextField(
                      controller: controller,
                      decoration: new InputDecoration(
                        contentPadding: EdgeInsets.only(top: -17.0),
                        hintText: '搜索',
                        border: InputBorder.none,
                        fillColor: Colors.white,
                        hoverColor: Colors.white,
                        filled: true,
                        hintStyle: TextStyle(color: Colors.black12),
                      ),
                      onChanged: onSearchTextChanged,
                    ),
                  ),
                  new IconButton(
                    icon: _searchText == '' ? const Icon(Icons.cancel) : const Icon(Icons.search),
                    color: Colors.grey,
                    hoverColor: Colors.white,
                    iconSize: 18.0,
                    onPressed: () {
                      if (_searchText == '') {
                        controller.clear();
                        this.widget.searchHandler(context, _searchText);
                        Navigator.of(context).pop();
                      } else {
                        this.widget.searchHandler(context, _searchText);
                      }
                    },
                  )
                ],
              )
          )
      ),
      Padding(
        padding: EdgeInsets.only(left: 5, right: 5),
        child: GestureDetector(
          child: Text('取消', style: TextStyle(color: Colors.green),),
          onTap: () {
            controller.clear();
            Navigator.of(context).pop();
          },
        ),
      ),
    ]);
  }

  @override
  void dispose(){
    controller.dispose();
    super.dispose();
  }

  void onSearchTextChanged(String text) {
    Future.delayed(Duration(milliseconds: 500), () {
      if (text == this._searchText) {
        this.widget.searchHandler(context, text);
      }
    });
    setState(() {
      this._searchText = text;
    });
  }

}